<template>
  <el-dialog
    title="驳回"
    :visible.sync="isShowReject"
    width="30%"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    center
  >
    <p class="title">驳回原因:</p>
    <el-input
      type="textarea"
      v-model="params.comment"
      autosize
      placeholder="请输入"
    />
    <div v-if="processObj.processName == '线下专家评审'">
      <el-upload
        style="margin-top: 20px"
        class="upload-demo"
        :action="uploadUrl"
        :on-success="handleSuccess"
        :headers="headers"
        :show-file-list="false"
      >
        <el-button size="small" type="primary">添加附件</el-button>
      </el-upload>

      <p v-for="(i, index) in params.filelist" :key="index">
        <span @click="openDoc(i)">{{ i.fileName }}</span>
        <i @click="onRemove(index)" class="el-icon-circle-close"></i>
      </p>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="determineFn(0)">取 消</el-button>
      <el-button type="danger" @click="determineFn(1)">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { uploadUrl } from "@/const";
import { getToken } from "@/utils/auth";
import { serverUrl } from "@/look";
import { Base64 } from "js-base64";

export default {
  props: {
    isShowReject: {
      type: Boolean,
      default: false,
    },
    processObj: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      uploadUrl,
      headers: {
        Authorization: getToken(),
        platform: 'WEB'
      },
      params: {
        approveResult: 1, // 驳回， 0 是通过
        comment: "",
        filelist:[],
        specialistAppendixUrl: [],
      },
    };
  },
  methods: {
    determineFn(i) {
      console.log(i);
      if (i == 1) {
        this.$emit("examineFn", i, this.params);
      } else {
        this.$emit("examineFn", i);
      }
      setTimeout(() => {
        this.params.filelist = [];
        this.params.specialistAppendixUrl = [];
      }, 1000);
    },
    // 文件上传成功
    handleSuccess(res) {
      console.log(res);
      let data = res.data;
      data.name = res.data.fileName;
      this.params.filelist.push(data);
      this.params.specialistAppendixUrl.push({
        fileName: data.fileName,
        url: data.url,
      });
    },
    // 附件预览
    openDoc(e) {
      window.open(
        serverUrl +
          "onlinePreview?url=" +
          encodeURIComponent(Base64.encodeURI(e.url))
      );
    },
    // 移除
    onRemove(index) {
      this.params.filelist.splice(index, 1);
      this.params.specialistAppendixUrl.splice(index, 1);
    },
  },
};
</script>

<style>
.el-dialog__header {
  text-align: left;
}
.title {
  font-size: 1rem;
  color: #333333;
  background: none;
}
</style>
